import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_community/utils/eventBus.dart';
import 'package:flutter_community/utils/http.dart';
import 'package:flutter_community/utils/showToast.dart';

class HouseDetail extends StatefulWidget {
  HouseDetail({super.key, required this.houseInfo});
  Map houseInfo;

  @override
  State<HouseDetail> createState() => _HouseDetailState();
}

class _HouseDetailState extends State<HouseDetail> {
  Map houseDetail = {};
  @override
  void initState() {
    super.initState();
    // houseDetail = widget.houseInfo;
    // print(houseDetail);
    getDetail(widget.houseInfo['id']);
  }

//获取详情数据
  getDetail(String id) async {
    var res = await http.get('/room/$id');
    setState(() {
      houseDetail = res['data'];
    });
    print(res);
  }

//删除房屋
  deleteHouse(String id) {
    return showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
              title: const Text('温馨提示'),
              content: const Text('您确定要删除吗？'),
              actions: [
                TextButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: const Text(
                      '取消',
                      style: TextStyle(color: Colors.grey),
                    )),
                TextButton(
                  onPressed: () async {
                    var res = await http.delete('/room/$id');
                    if (res['code'] == 10000) {
                      ToastMessage.showSuccess('删除成功');
                      Navigator.pop(context);
                      eventBus.fire(RefreshMyHouseEvent());
                    } else {
                      ToastMessage.showFail(res['message']);
                    }
                  },
                  child: const Text('确定'),
                )
              ]);
        });
  }

  //右侧审核状态
  Widget _buildTagItem(int status) {
    List tag = [
      {
        "name": "审核中",
        'bgcolor': const Color.fromARGB(255, 162, 208, 246),
        "textcolor": const Color.fromARGB(255, 171, 244, 249)
      },
      {
        "name": "审核通过",
        'bgcolor': const Color.fromARGB(255, 31, 184, 59),
        "textcolor": const Color.fromARGB(255, 123, 239, 173)
      },
      {
        "name": "审核失败",
        'bgcolor': const Color.fromARGB(255, 242, 81, 81),
        "textcolor": const Color.fromARGB(255, 246, 149, 164)
      },
    ];
    return Container(
      width: 65,
      padding: const EdgeInsets.all(1),
      decoration: BoxDecoration(
        color: tag[status]['bgcolor'],
        borderRadius: BorderRadius.circular(5),
      ),
      //审核状态
      child: Text(
        tag[status]['name'],
        style: TextStyle(color: tag[status]['textcolor']),
        textAlign: TextAlign.center,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('房屋详情'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: houseDetail.isEmpty
          ? const Center(
              child: CircularProgressIndicator(),
            )
          : Stack(
              children: [
                //顶部
                Container(
                  padding: const EdgeInsets.all(10),
                  child: ListView.builder(
                    padding: const EdgeInsets.only(top: 10, bottom: 50),
                    itemCount: 1,
                    itemBuilder: (BuildContext context, int index) {
                      return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          //房屋信息
                          const Text('房屋信息',
                              style:
                                  TextStyle(fontSize: 18, color: Colors.grey)),
                          const SizedBox(
                            height: 10,
                          ),
                          Container(
                              color: Colors.white,
                              height: 60,
                              child: Container(
                                padding: const EdgeInsets.all(10),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text('${houseDetail['point']}'),
                                    _buildTagItem(houseDetail['status'] - 1)
                                  ],
                                ),
                              )),
                          const SizedBox(
                            height: 10,
                          ),
                          //业主信息
                          const Text(
                            '业主信息',
                            style: TextStyle(fontSize: 18, color: Colors.grey),
                          ),
                          const SizedBox(
                            height: 10,
                          ),
                          Container(
                              color: Colors.white,
                              // height: 200,
                              child: Padding(
                                  padding: const EdgeInsets.all(10),
                                  child: Column(
                                    children: [
                                      //姓名
                                      Row(
                                        children: [
                                          const Text(
                                            '房间号',
                                            style: TextStyle(fontSize: 16),
                                          ),
                                          const SizedBox(
                                            width: 40,
                                          ),
                                          Text(
                                            '${houseDetail['building']}${houseDetail['room']}室',
                                            style:
                                                const TextStyle(fontSize: 16),
                                          )
                                        ],
                                      ),

                                      const SizedBox(
                                        height: 10,
                                      ),
                                      //业主
                                      Row(
                                        children: [
                                          const Text(
                                            '业主',
                                            style: TextStyle(fontSize: 16),
                                          ),
                                          const SizedBox(
                                            width: 55,
                                          ),
                                          Text(
                                            houseDetail['name'],
                                            style:
                                                const TextStyle(fontSize: 16),
                                          )
                                        ],
                                      ),
                                      const SizedBox(
                                        height: 10,
                                      ),
                                      //手机号
                                      Row(
                                        children: [
                                          const Text(
                                            '手机号',
                                            style: TextStyle(fontSize: 16),
                                          ),
                                          const SizedBox(
                                            width: 40,
                                          ),
                                          Text(
                                            houseDetail['mobile'],
                                            style:
                                                const TextStyle(fontSize: 16),
                                          )
                                        ],
                                      )
                                    ],
                                  ))),

                          //本人身份证照片
                          Container(
                            margin: const EdgeInsets.only(top: 10, bottom: 10),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                const Text(
                                  '本人身份证照片',
                                  style: TextStyle(
                                      fontSize: 18, color: Colors.grey),
                                ),
                                const SizedBox(
                                  height: 10,
                                ),
                                const Text('请拍摄证件原件,并使照片中证件边缘完整,文字清晰,光面均匀'),
                                const SizedBox(
                                  height: 10,
                                ),
                                //正面
                                Container(
                                  color: Colors.white,
                                  height: 300,
                                  margin: const EdgeInsets.only(
                                      bottom: 10, top: 10),
                                  width: MediaQuery.of(context).size.width,
                                  child: Image.network(
                                      '${houseDetail['idcardFrontUrl']}',
                                      height: 160),
                                ),

                                //反面
                                Container(
                                  color: Colors.white,
                                  height: 300,
                                  margin: const EdgeInsets.only(
                                      bottom: 10, top: 10),
                                  width: MediaQuery.of(context).size.width,
                                  child: Image.network(
                                      '${houseDetail['idcardBackUrl']}',
                                      height: 160),
                                )
                              ],
                            ),
                          ),
                        ],
                      );
                    },
                  ),
                ),
                //底部按钮
                Positioned(
                    bottom: 0,
                    left: 0,
                    right: 0,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        //删除房屋
                        Container(
                            padding: const EdgeInsets.all(10),
                            // width: 200,
                            width: MediaQuery.of(context).size.width / 2,
                            // color: Colors.blue,
                            child: ElevatedButton(
                              onPressed: () {
                                deleteHouse(houseDetail['id']);
                              },
                              child: const Padding(
                                padding: EdgeInsets.all(8),
                                child: Column(
                                  children: [
                                    Icon(Icons.delete),
                                    SizedBox(
                                      height: 5,
                                    ),
                                    Text('删除房屋'),
                                  ],
                                ),
                              ),
                            )),
                        //修改房屋
                        Container(
                            padding: const EdgeInsets.all(10),
                            width: MediaQuery.of(context).size.width / 2,
                            // color: Colors.blue,
                            child: ElevatedButton(
                              onPressed: () {
                                //跳转到添加房屋页
                                Navigator.pushNamed(context, '/house_form',
                                    arguments: houseDetail);
                              },
                              child: const Padding(
                                padding: EdgeInsets.all(8),
                                child: Column(
                                  children: [
                                    Icon(Icons.edit),
                                    SizedBox(
                                      height: 5,
                                    ),
                                    Text('修改房屋'),
                                  ],
                                ),
                              ),
                            ))
                      ],
                    )),
              ],
            ),
    );
  }
}
